﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Project.Database.Repository
{
    public partial class DatabaseRepository
    {
        #region Properties
        #endregion

        #region Query Methods

        public IQueryable<Advert> GetAllAdverts()
        {
            return from advert in entities.Adverts
                   orderby advert.AdvertiserID
                   select advert;
        }

        public IQueryable<Advert> GetRecentAdverts(int count = 10)
        {
            return (from advert in entities.Adverts
                   orderby advert.Timestamp
                   select advert).Take(count);
        }

        public IQueryable<Advert> GetRecentAdvertsForAdvertiser(int advertiserID, int count = 10)
        {
            return (from advert in entities.Adverts
                    where advert.AdvertiserID == advertiserID
                    orderby advert.Timestamp
                    select advert).Take(count);
        }

        public IQueryable<Advert> GetRecentAdvertsForAdvertiser(Advertiser advertiser, int count = 10)
        {
            return GetRecentAdvertsForAdvertiser(advertiser.ID, count);
        }

        public IQueryable<Advert> GetAdvertsForAdvertiser(Advertiser advertiser)
        {
            return GetAdvertsForAdvertiser(advertiser.ID);
        }

        public IQueryable<Advert> GetAdvertsForAdvertiser(int advertiserID)
        {
            return from advert in entities.Adverts
                   where advert.AdvertiserID == advertiserID
                   select advert;
        }

        public IQueryable<Advert> GetAdvertsForCampaign(AdCampain campaign)
        {
            campaign.Adverts.Load();
            return campaign.Adverts.AsQueryable();
        }

        public IQueryable<Advert> GetAdvertsOnBlog(Blog blog)
        {
            blog.Adverts.Load();
            return blog.Adverts.AsQueryable();
        }

        public Advert GetAdvert(int id)
        {
            return entities.Adverts.SingleOrDefault(advert => advert.ID == id);
        }

        public Advert GetAdvert(string URL)
        {
            return entities.Adverts.SingleOrDefault(advert => advert.URL == URL);
        }

        #endregion

        #region Insert/Delete

        private void AddAdvert(Advert advert)
        {
            if (AdvertExists(advert))
                throw new ArgumentException("Advert already exists!");

            entities.Adverts.AddObject(advert);
        }


        public void CreateAdvert(string name, int advertiserID, string URL, string targetURL, int campaignID, int categoryID, int adtypeID, int countryID, string keywords, decimal costToAdvertiser, decimal gainToBlogger)
        {
            //TODO check input strings and category ID

            Advert advert = new Advert()
            {
                Name = name.Trim(),
                URL = URL.Trim(),
                TargetURL = targetURL,
                AdTypeID = adtypeID,
                CampaignID = campaignID,
                CostToAdvertiser = costToAdvertiser,
                GainToBlogger = gainToBlogger,
                AdvertiserID = advertiserID,
                Timestamp = DateTime.Now,
                Keywords = keywords,
                CountryID = countryID,
                CategoryID = categoryID
            };



            try
            {
                AddAdvert(advert);
                // Immediately persist the user data
                Save();
            }
            catch (ArgumentException ae)
            {
                throw ae;
            }
            catch (Exception e)
            {
                Console.Write(e.Message); // ADD tHIS TO ALL ERRORS
                throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
                    "If the problem persists, please contact your system administrator.");
            }
        }

        public void DeleteAdvert(Advert advert)
        {
            if (!AdvertExists(advert))
                throw new ArgumentException("Advert does not exists");

            entities.Adverts.DeleteObject(advert);
        }

        #endregion

        #region Helper Methods

        public bool AdvertExists(Advert advert)
        {
            if (advert == null)
                return false;

            return (entities.Adverts.SingleOrDefault(a => a.ID == advert.ID) != null);
        }

        #endregion
    }
}